package com.landmesure.communication;

import com.code.EntityType;
import com.code.EnumValue;
import com.code.JsonToJavabean;
import com.code.MonitorItem;

import com.code.PropertyDefinition;
import com.landmesure.core.TestMessage;
import com.landmesure.utils.PropertyBuilder;

import java.lang.reflect.Field;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

public class MonitorItemDecode {
    public static List<TestMessage> decodeMonitorItem(String str) throws ParseException {
        List<TestMessage> datas=new ArrayList<>();
        int offset = 30;
        int length = Integer.parseInt(str.substring(4, 6), 16);
        //int target = Integer.parseInt(str.substring(6, 10), 16);//站号板号
        int ecode = Integer.parseInt(str.substring(26, 30), 16);//对象编号
        // int pcode = Integer.parseInt(str.substring(24, 26), 16);//工程号
        MonitorItem monitorItem;
    /*    StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(str).append("\n");
        stringBuilder.append("工程号：").append(pcode).append("\n");
        stringBuilder.append("站号：").append(target >> 6).append("\n");
        stringBuilder.append("板号：").append(target & 0x003F).append("\n");
        stringBuilder.append("对象编号：").append(ecode).append("\n\n");
        stringBuilder.append("数据内容\n");*/


        while (offset < length * 2 + 22) {
            int code = Integer.parseInt(str.substring(offset, offset += 2), 16);
         //   Log.i(TAG, "yuan数据类型: "+code);
            monitorItem = JsonToJavabean.monitorItemByCode(code);
            if (monitorItem != null) {
                String tempValue = str.substring(offset, offset += (monitorItem.getDataLength() * 2));
                String value;
                Field[] fields = monitorItem.getClass().getDeclaredFields();
                boolean b = false;
                for (Field field : fields) {
                    if (field.getName().equals("decimalLength")) {
                        b = true;
                        break;
                    }
                }
                if (code == 244) {
                    value = PropertyBuilder.DatetimeBuilder.decode(tempValue, monitorItem.getDataLength());
                } else if (monitorItem.getName().equals("数字信号")) {
                    value = tempValue;
                } else if (b) {
                    value = PropertyBuilder.FloatBuilder.decode(tempValue, monitorItem.getDataLength());
                } else {
                    value = PropertyBuilder.IntegerBuilder.decode(tempValue, monitorItem.getDataLength());
                }
                TestMessage testMessage=new TestMessage(ecode,monitorItem.getName(),monitorItem.getUnit()==null?"":monitorItem.getUnit(),value);
                datas.add(testMessage);
            }


        }
        return datas;


    }

    static String decodeParameter(String str, EntityType entityType, ArrayList arrayList)   {
        int offset = 40;
       // int length = Integer.parseInt(str.substring(4, 6), 16);
        int target = Integer.parseInt(str.substring(6, 10), 16);//站号板号
        int ecode = Integer.parseInt(str.substring(26, 30), 16);//对象编号
        int pcode = Integer.parseInt(str.substring(24, 26), 16);//工程号
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(str).append("\n");
        stringBuilder.append("工程号：").append(pcode).append("\n");
        stringBuilder.append("站号：").append(target >> 6).append("\n");
        stringBuilder.append("板号：").append(target & 0x003F).append("\n");
        stringBuilder.append("对象编号：").append(ecode).append("\n\n");
        stringBuilder.append("数据内容\n");
        for (PropertyDefinition p : entityType.getPropertyDefinitions()
                ) {
            if (p.getCode() == Integer.parseInt((String) arrayList.get(0))) {
                stringBuilder.append(p.getName()).append(":").append(switchMessage(p.getVtype(), str.substring(offset, offset + p.getDataLength() * 2), p.getDataLength(), p));
            }
        }
        return stringBuilder.toString();
    }

    private static  String switchMessage(String type, String tempValue, int dataLength, PropertyDefinition p) {
        //参数值解码
        String returnMessage=null;
        switch (type) {
            case "integer":
                if(p.isEnumable()){
                    for (EnumValue enumValue:p.getEnumValues()
                         ) {
                      //  String i=PropertyBuilder
                       //         .IntegerBuilder.decode(tempValue, dataLength);
                        if(enumValue.getCode().equals(PropertyBuilder
                                .IntegerBuilder.decode(tempValue, dataLength))){
                            returnMessage=enumValue.getValue();
                        }
                    }
                }
                else {
                returnMessage = PropertyBuilder
                        .IntegerBuilder.decode(tempValue, dataLength);
                }
                break;
            case "double":
                if(p.isEnumable()){
                    for (EnumValue enumValue:p.getEnumValues()
                            ) {
                     /*   String i=PropertyBuilder
                                .FloatBuilder.decode(tempValue, dataLength);*/
                        if(enumValue.getCode().equals(PropertyBuilder
                                .FloatBuilder.decode(tempValue, dataLength))){
                            returnMessage=enumValue.getValue();
                        }
                    }
                }
                else {
                    returnMessage = PropertyBuilder
                            .FloatBuilder.decode(tempValue, dataLength);
                }
                break;
            case "boolean":
                returnMessage = PropertyBuilder
                        .BooleanBuilder.decode(tempValue, dataLength);
                break;
            case "string":
                returnMessage = PropertyBuilder
                        .AscBuilder.decode(tempValue, dataLength);
                break;
            case "time":
                returnMessage = PropertyBuilder
                        .TimeBuilder.decode(tempValue, dataLength);
                break;
            case "datetime":
                try {
                    returnMessage = PropertyBuilder
                            .DatetimeBuilder.decode(tempValue, dataLength);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                break;
            default:
                returnMessage = tempValue;
        }
        return returnMessage;
    }
}